﻿
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Configuration;
using System.Data.SqlClient;
using System.Data;
using Website.Logger;
using DAL;
using BOL;
using System.Web.Script.Services;
using System.Web.Services;
using WebLib;


namespace UILGestion.Admin.Insumo
{
    public partial class InsumoListForm : System.Web.UI.Page
    {

        [WebMethod(), ScriptMethod()]
        public static string GetRecursos(String prefixText, String contextKey)
        {

            var insumobl = new InsumoBL();
            
            return insumobl.getInsumoAll(prefixText, 25);

        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                try
                {
                    //idTipoInsumo = int.Parse((string)Request.QueryString["idTipoInsumo"]);
                    menuCtrl1.loadMenuAdmin();
                    FormatForm();
                }
                catch (SqlException sqlex)
                {
                    WebServerSideSqlError.Log(sqlex);

                    ScriptManager.RegisterClientScriptBlock(this, typeof(Page),
                     "uniqueKey" + DateTime.Now, "alert('" + sqlex.Message.Replace("'", "") + "');", true);


                }
                catch (Exception ex)
                {
                    WebServerSideError.Log(ex);
                    ScriptManager.RegisterClientScriptBlock(this, typeof(Page),
                         "uniqueKey" + DateTime.Now, "alert('" + ex.Message.Replace("'", "") + "');", true);

                }

            }
        }
        // private void restricionesForm()
        //{

        //    if (!((string)Session["rolid"] == "1"))
        //    {

        //        proyectoDropDownList.Enabled =
        //                                    false;
        //        proyectoDropDownList.SelectedValue ="1";
        //                                    //Session["idInt"].ToString();

        //    }

        //}
        private void FormatForm()
        {



            ShowAllComboBox();
            //restricionesForm();
          
            Find();

            string Lang = "es-ES";  //para controlar el idioma 
            System.Threading.Thread.CurrentThread.CurrentCulture = new System.Globalization.CultureInfo
   (Lang);

           

        }
        protected void ShowAllComboBox()
        {

            insumoDDL.DataSource = new ListValueBL().GetInsumoListNiveles();
            insumoDDL.DataBind();


            //claseDropDownList.Items.Clear();
            //familiaDropDownList.Items.Clear();
            ////subFamiliaDropDownList.Items.Clear();

            //claseDropDownList.DataTextField = "descripcion";
            //claseDropDownList.DataValueField = "idInternal";
            
            //familiaDropDownList.DataTextField = "descripcion";
            //familiaDropDownList.DataValueField = "idInternal";

            //subfamiliaDropDownList.DataTextField = "descripcion";
            //subfamiliaDropDownList.DataValueField = "idInternal";
            
            ////mostras todas las clases

            //ListValueBL bol = new ListValueBL();
            //List<ListValueInfo> lista = new List<ListValueInfo>();
            //ListValueInfo insumo = new ListValueInfo();
            //insumo.idInternal = 0;
            //insumo.descripcion = "-----Todas las Clases-----";

            //lista.Add(insumo);
            //lista.AddRange(bol.GetInsumoClases(idTipoInsumo));

            //claseDropDownList.DataSource = lista;
            //claseDropDownList.DataBind();

            ////mostras las familias
            //List<ListValueInfo> insumofamiliaList = new List<ListValueInfo>();
            
            //ListValueInfo insumofamilia = new ListValueInfo();
            //insumofamilia.idInternal = 0;
            //insumofamilia.descripcion = "-----Todas las familias-----";

            //insumofamiliaList.Add(insumofamilia);
            //insumofamiliaList.AddRange(bol.GetInsumoFamilia(idTipoInsumo));

            //familiaDropDownList.DataSource = insumofamiliaList;
            //familiaDropDownList.DataBind();


            ////mostras las subfamilias

            ////var subfamilias = from a in _db.view_Insumo
            ////               where a.nivel == 1
            ////               select a;


            //List<ListValueInfo> insumosubfamiliaList = new List<ListValueInfo>();
            //ListValueInfo insumosubfamilia = new ListValueInfo();
            //insumosubfamilia.idInternal = 0;
            //insumosubfamilia.descripcion = "-----Todos subfamilias-----";

            //insumosubfamiliaList.Add(insumosubfamilia);
            //insumosubfamiliaList.AddRange(bol.GetInsumoSubFamilia(idTipoInsumo));

            //subfamiliaDropDownList.DataSource = insumosubfamiliaList;
            //subfamiliaDropDownList.DataBind();
            
            
        }
       
        //protected void claseDropDownList_SelectedIndexChanged(object sender, EventArgs e)
        //{
        //    try
        //    {

        //        if (claseDropDownList.Items.Count > 0)
        //        {
        //            InsumoBL bol = new InsumoBL();
        //            if (claseDropDownList.SelectedValue != "0")
        //            {
        //                InsumoListGrid1.DataSource = bol.getInsumoListXClase(int.Parse(claseDropDownList.SelectedValue), idTipoInsumo);
        //                InsumoListGrid1.DataBind();
        //                familiaDropDownList.SelectedValue = "0";
        //            }
        //            else
        //            {
        //                InsumoListGrid1.DataSource = bol.getInsumoAllClases(idTipoInsumo);
        //                InsumoListGrid1.DataBind();
        //                familiaDropDownList.SelectedValue = "0";
        //            }

        //        }

        //    }
        //    catch (SqlException sqlex)
        //    {
        //        WebServerSideSqlError.Log(sqlex);
        //        ScriptManager.RegisterClientScriptBlock(this, typeof(Page),
        //            "uniqueKey" + DateTime.Now, "alert('" + sqlex.Message.Replace("'", "") + "');", true);



        //    }
        //    catch (Exception ex)
        //    {
        //        WebServerSideError.Log(ex);
        //        ScriptManager.RegisterClientScriptBlock(this, typeof(Page),
        //            "uniqueKey" + DateTime.Now, "alert('" + ex.Message.Replace("'", "") + "');", true);





        //    }
        //}
        //protected void familiaDropDownList_SelectedIndexChanged(object sender, EventArgs e)
        //{
        //    try
        //    {
        //        if (familiaDropDownList.Items.Count > 0)
        //        {
        //            InsumoBL bol = new InsumoBL();
        //            if (familiaDropDownList.SelectedValue != "0")
        //            {
        //                InsumoListGrid1.DataSource = bol.getInsumoListXFamilia(int.Parse(familiaDropDownList.SelectedValue), idTipoInsumo);
        //                InsumoListGrid1.DataBind();
        //                claseDropDownList.SelectedValue = "0";
        //            }
        //            else
        //            {
        //                InsumoListGrid1.DataSource = bol.getInsumoAllfamilias(idTipoInsumo);
        //                InsumoListGrid1.DataBind();
        //                    claseDropDownList.SelectedValue = "0";
        //            }
        //        }
        //    }
        //    catch (SqlException sqlex)
        //    {
        //        WebServerSideSqlError.Log(sqlex);
        //        ScriptManager.RegisterClientScriptBlock(this, typeof(Page),
        //            "uniqueKey" + DateTime.Now, "alert('" + sqlex.Message.Replace("'", "") + "');", true);



        //    }
        //    catch (Exception ex)
        //    {
        //        WebServerSideError.Log(ex);
        //        ScriptManager.RegisterClientScriptBlock(this, typeof(Page),
        //            "uniqueKey" + DateTime.Now, "alert('" + ex.Message.Replace("'", "") + "');", true);





        //    }
        //}
        //protected void subfamiliaDropDownList_SelectedIndexChanged(object sender, EventArgs e)
        //{
        //    try
        //    {
        //        if (subfamiliaDropDownList.Items.Count > 0)
        //        {
        //            InsumoBL bol = new InsumoBL();
        //            if (familiaDropDownList.SelectedValue != "0")
        //            {
        //                InsumoListGrid1.DataSource = bol.getInsumoListXSubFamilia(int.Parse(familiaDropDownList.SelectedValue), idTipoInsumo);
        //                InsumoListGrid1.DataBind();
        //                claseDropDownList.SelectedValue = "0";
        //                familiaDropDownList.SelectedValue = "0";
        //            }
        //            else
        //            {
        //                InsumoListGrid1.DataSource = bol.getInsumoAllsubfamilias(idTipoInsumo);
        //                InsumoListGrid1.DataBind();
        //                    claseDropDownList.SelectedValue = "0";
        //                    familiaDropDownList.SelectedValue = "0";
                    
                    
        //            }



        //        }
        //    }
        //    catch (SqlException sqlex)
        //    {
        //        WebServerSideSqlError.Log(sqlex);
        //        ScriptManager.RegisterClientScriptBlock(this, typeof(Page),
        //            "uniqueKey" + DateTime.Now, "alert('" + sqlex.Message.Replace("'", "") + "');", true);



        //    }
        //    catch (Exception ex)
        //    {
        //        WebServerSideError.Log(ex);
        //        ScriptManager.RegisterClientScriptBlock(this, typeof(Page),
        //            "uniqueKey" + DateTime.Now, "alert('" + ex.Message.Replace("'", "") + "');", true);





        //    }
        //}

       


        private void Find()
        {

               //InsumoBL bol= new InsumoBL();
               //InsumoListGrid1.DataSource = bol.getInsumoAllClases(idTipoInsumo);
               //InsumoListGrid1.DataBind();
               //claseDropDownList.SelectedValue="0";
              

               
        
        }
           

        protected void nuevoButton_Click(object sender, EventArgs e)
        {
            try
            {
                Response.Redirect("InsumoInsertForm.aspx?type=A");

            }
            catch (SqlException sqlex)
            {
                WebServerSideSqlError.Log(sqlex);
                ScriptManager.RegisterClientScriptBlock(this, typeof(Page),
                    "uniqueKey" + DateTime.Now, "alert('" + sqlex.Message.Replace("'", "") + "');", true);



            }
            catch (Exception ex)
            {
                WebServerSideError.Log(ex);
                ScriptManager.RegisterClientScriptBlock(this, typeof(Page),
                    "uniqueKey" + DateTime.Now, "alert('" + ex.Message.Replace("'", "") + "');", true);





            }
        }

        protected void actualizarButton_Click(object sender, EventArgs e)
        {
            try
            {
                Find();

            }
            catch (SqlException sqlex)
            {
                WebServerSideSqlError.Log(sqlex);
                ScriptManager.RegisterClientScriptBlock(this, typeof(Page),
                    "uniqueKey" + DateTime.Now, "alert('" + sqlex.Message.Replace("'", "") + "');", true);



            }
            catch (Exception ex)
            {
                WebServerSideError.Log(ex);
                ScriptManager.RegisterClientScriptBlock(this, typeof(Page),
                    "uniqueKey" + DateTime.Now, "alert('" + ex.Message.Replace("'", "") + "');", true);





            }
        }

       
        protected void CancelEditItem(object sender, GridViewCancelEditEventArgs e)
        {
            try
            {
                //itemGridView.EditIndex = -1;
                //DataBindItems();
                //itemGridView.FooterRow.Visible = true;

            }
            catch (SqlException sqlex)
            {
                WebServerSideSqlError.Log(sqlex);
                ScriptManager.RegisterClientScriptBlock(this, typeof(Page),
                 "uniqueKey" + DateTime.Now, "alert('" + sqlex.Message.Replace("'", "") + "');", true);


            }
            catch (Exception ex)
            {
                WebServerSideError.Log(ex);
                ScriptManager.RegisterClientScriptBlock(this, typeof(Page),
                 "uniqueKey" + DateTime.Now, "alert('" + ex.Message.Replace("'", "") + "');", true);

            }

        }
        protected void UpdateItem(object sender, GridViewUpdateEventArgs e)
        {
            try
            {

                //string idInternal = ((GridView)sender).DataKeys[e.RowIndex].Values[0].ToString();
                //string cantidad = ((System.Web.UI.WebControls.TextBox)itemGridView.Rows[e.RowIndex].FindControl("cantidadEditTextBox")).Text;

                //NotaIngresoItemInfo ItemsInfo = new NotaIngresoItemInfo();
                //DataSet recursos = (DataSet)Session[idColeccionList.Value];

                //DataRow drw = (from a in recursos.Tables[ItemsInfo.GetType().Name].AsEnumerable().Where(p => p.RowState != DataRowState.Deleted)
                //               where a.Field<int>("idInternal") == int.Parse(idInternal)
                //               select a).First();
                //ItemsInfo = (NotaIngresoItemInfo)Util.DataRowToEntidad(drw, ItemsInfo.GetType());

                //ItemsInfo.cantidad = decimal.Parse(cantidad);

                //Util.EntidadUpdateDataRow(ItemsInfo, drw);

                //Session[idColeccionList.Value] = recursos;
                //itemGridView.EditIndex = -1;
                ////itemGridView.FooterRow.Visible = true; solo editar
                //DataBindItems();

            }
            catch (SqlException sqlex)
            {
                WebServerSideSqlError.Log(sqlex);
                ScriptManager.RegisterClientScriptBlock(this, typeof(Page),
                 "uniqueKey" + DateTime.Now, "alert('" + sqlex.Message.Replace("'", "") + "');", true);


            }
            catch (Exception ex)
            {
                WebServerSideError.Log(ex);
                ScriptManager.RegisterClientScriptBlock(this, typeof(Page),
                 "uniqueKey" + DateTime.Now, "alert('" + ex.Message.Replace("'", "") + "');", true);

            }

        }

        protected void Deleteitem(object sender, EventArgs e)
        {
            try
            {

                System.Web.UI.WebControls.ImageButton lnkRemove = (System.Web.UI.WebControls.ImageButton)sender;
                int idInternal = int.Parse(lnkRemove.CommandArgument.ToString());

                InsumoBL bl = new InsumoBL();
                bl.delete(idInternal);
               
                
               // DataBindItems();



            }
            catch (SqlException sqlex)
            {
                WebServerSideSqlError.Log(sqlex);
                ScriptManager.RegisterClientScriptBlock(this, typeof(Page),
                 "uniqueKey" + DateTime.Now, "alert('" + sqlex.Message.Replace("'", "") + "');", true);


            }
            catch (Exception ex)
            {
                WebServerSideError.Log(ex);
                ScriptManager.RegisterClientScriptBlock(this, typeof(Page),
                 "uniqueKey" + DateTime.Now, "alert('" + ex.Message.Replace("'", "") + "');", true);

            }

        }

        }
}
